const {getCarDetail, getCarList, newCar, delCar, updateCar, getNoReversedCar, cancelUpdateCar, resetCar} = require("../controller/car");
const {updateCoachUsername, getCoachByCarNumber, cancelReverseCar, resetReverseCar} = require("../controller/coach");
const Response = require('../common/Response')
const router = require('koa-router')()

router.prefix('/api/car')

// 获取所有车辆
router.get('/list', async (context, next) => {
  const rows = await getCarList()
  context.body = Response.success(rows)
})
// 添加车辆
router.post('/new', async (context, next) => {
  const val = await newCar(context.request.body)
  if (val) {
    context.body = Response.success('添加车辆成功')
  } else {
    context.body = Response.failure('添加车辆失败')
  }
})
// 修改车辆
router.post('/update', async (context, next) => {
  const val = await updateCar(context.request.body)
  if (val) {
    context.body = Response.success('更新车辆成功')
  } else {
    context.body = Response.failure('更新车辆失败')
  }
})
// 删除车辆
router.post('/del/:id', async (context, next) => {
  const val = await delCar(context.params.id)
  if (val) {
    context.body = Response.success('删除车辆成功')
  } else {
    context.body = Response.failure('删除车辆失败')
  }
})
// 分配教练
router.post('/assigned', async (context, next) => {
  const CarValue = await updateCar(context.request.body.car)
  if (CarValue) {
    const CoachValue = await updateCoachUsername(context.request.body.coach_identify, context.request.body.car.car_number)
    if (CoachValue) {
      context.body = Response.success('车辆分配成功')
    }
  } else {
    context.body = Response.failure('车辆分配失败')
  }
})
// 取消分配
router.post('/unassigned', async (context, next) => {
  const CarValue = await cancelUpdateCar(context.request.body)
  if (CarValue) {
    const CoachInfo = await getCoachByCarNumber(context.request.body.car_number)
    if (CoachInfo.identify) {
      const CoachValue = await cancelReverseCar(CoachInfo.identify)
      if (CoachValue) {
        context.body = Response.success('车辆取消分配成功')
      }
    }
  } else {
    context.body = Response.failure('车辆取消分配失败')
  }
})

// 获取未预约车辆信息
router.get('/not-use', async (context, next) => {
  const rows = await getNoReversedCar()
  context.body = Response.success(rows)
})
// 获取车辆详细信息（未用到）
router.get('/detail', async (context, next) => {
  const row = await getCarDetail(context.request.body.car_number)
  context.body = Response.success(row)
})
// 新的一天来临，完成车辆预约时间重置
router.get('/reset', async (context, next) => {
  // 1. 获取所有车辆
  const cars = await getCarList();
  // 2. 获取有预约日期的车辆
  const cars_appointe = cars.filter(item => item.appoint_date)
  // 3. 获取预约日期已过的车辆
  const cars_appointed = cars_appointe.filter(item => {
    return new Date(item.appoint_date).getTime() < Date.now()
  })
  // 4. 获取预约日期已过车辆的id
  const arr = cars_appointed.map(item => item.id)
  if (arr.length > 0) {
    //  重置有被预约的车辆
    const val = await resetCar(arr)
    // 重置预约车辆的教练
    if (val) {
      // 重置教练预约车辆
      await resetReverseCar()
      context.body = Response.success()
    } else {
      context.body = Response.failure()
    }
  } else {
    context.body = Response.success()
  }
})

module.exports = router
